var common = require("../../utils/util.js");
var permission = true;
var myDate = new Date();
var date = myDate.getDate();
var month = myDate.getMonth() + 1;
var tomorrow = date + 1;
var time = false;

Page({
  /**
   * 页面的初始数据
   */
  data: {
    phone: "",
    time: "",
    date: "",
    multiSelectorData: [
      ['今天', '明天'],
      [],
      ['00']
    ],
    showTime: "请选择购买时间",
    params: {},
    tip: {
      title: '请填写要购买的商品',
      buy_address: '请选择购买地址',
      rec_address: '请选择收货地址',
      rec_name: '请填写收货人姓名',
      rec_phone: '请填写收货人手机号',
      order_time: '请选择购买时间',
      price: '请填写商品价格',
      reward: '请填写辛苦费',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let i = 0;
    let j = 1;
    let hours = 6;
    let minutes = 10;
    while (hours < 24) {
      let key = "multiSelectorData[1][" + i + "]";
      this.setData({
        [key]: hours,
      })
      hours++;
      i++;
    }
    while (minutes < 60) {

      let key2 = "multiSelectorData[2][" + j + "]";
      this.setData({
        [key2]: minutes,
      })
      minutes = minutes + 10;
      j++;
    }
  },


  getBuy(e) {
    this.setData({
      ['params.title']: e.detail.value
    })
  },
  openMap(e) {
    let key = e.currentTarget.dataset.key
    if (permission) {
      wx.chooseLocation({
        success: (res) => {
          let keyAddress = `params.${key}_address`,
            keyLat = `params.${key}_lat`,
            keyLon = `params.${key}_lng`;
          this.setData({
            [keyAddress]: res.name,
            [keyLat]: res.latitude,
            [keyLon]: res.longitude,
          })
        },
        fail: (err) => {
          console.log("授权失败", err);
          console.log(err.errMsg)
          if (err.errMsg == "chooseLocation:fail auth deny") {
            permission = false;
          }
        }
      })
    } else {
      wx.openSetting({
        success: (res) => {
          console.log(res.authSetting["scope.userLocation"])
          if (res.authSetting["scope.userLocation"]) {
            permission = true;
          }
        }
      })
    }
  },
  timeChange(e) {
    var one = e.detail.value[0];
    var two = e.detail.value[1];
    var three = e.detail.value[2];
    if (this.data.multiSelectorData[0][one] == "今天") {
      this.data.date = date;
    } else {
      if (month == 2) {
        if (date == 28) {
          tomorrow = 1;
        }
      } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
        if (date == 31) {
          tomorrow = 1;
        }
      } else {
        if (date == 30) {
          tomorrow = 1;
        }
      }
      this.data.date = tomorrow;
    }
    var thone = this.data.multiSelectorData[0][one];
    var thtwo = this.data.multiSelectorData[1][two];
    var ththree = this.data.multiSelectorData[2][three];
    //用来检查用户是否选择了时间
    time = true;
    let dateTime = this.getDateTime(one);
    this.setData({
      showTime: thone + " " + thtwo + ":" + ththree,
      'params.order_time': `${dateTime} ${thtwo}:${ththree}`,
    })
  },
  getDateTime(index) {
    var day = new Date();
    if (index == 1) {
      day.setTime(day.getTime() + 24 * 60 * 60 * 1000);
    } else {
      day.setTime(day);
    }
    let str = day.getFullYear() + "-" + (day.getMonth() + 1) + "-" + day.getDate();
    return str;
  },
  addOrder(e) {
    wx.showLoading({
      title: "加载中",
      mask: true
    });
    let data = e.detail.value;
    let params = {
      ...this.data.params,
      ...data
    }
    for (let key in this.data.tip) {
      if (!params[key]) {
        wx.hideLoading();
        wx.showToast({
          title: this.data.tip[key],
          icon: "none"
        });
        return;
      }
    }
    console.log('params', params);
    common.getData_user(common.config.AddOrder, params, 'post', res => {
      wx.hideLoading();
        wx.showToast({
          title: '发布成功',
          icon: "none"
        });
        wx.navigateBack();
    });

  },
})